package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Cv;
import com.zpark.resume.entity.Recommend;
import com.zpark.resume.exception.FindCvByIdIsNullException;
import com.zpark.resume.exception.InsertRecommendLoseException;
import com.zpark.resume.exception.UpdateRecommendLoseException;
import com.zpark.resume.mapper.IRecommendMapper;
import com.zpark.resume.service.IRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: RecommendServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/6/1  17:39
 */
@Service
public class RecommendServiceImpl implements IRecommendService {

    @Autowired
    private IRecommendMapper recommendMapper;

    /**
     * 根据member的id查询数据
     * @param memberId
     * @return
     */
    @Override
    public Recommend findByMemberId(Integer memberId) {
        // 调用持久层接口查询数据
        Recommend recommend = recommendMapper.selectByMemberId(memberId);
        // 判断查询结果
        if (recommend == null) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return recommend;
    }

    @Override
    public Integer addRecommend(Recommend recommend, Integer memberId) {
        // 判断recommend是否为null
        if (recommend == null) {
            throw new InsertRecommendLoseException("添加自荐信异常");
        }
        // 完善recommend数据
        recommend.setMemberId(memberId);
        recommend.setCreatedTime(new Date());
        recommend.setUpdateTime(new Date());
        recommend.setStatus(1);

        // 调用持久层接口添加数据
        Integer row = recommendMapper.insertRecommend(recommend);
        // 判断执行结果
        if (row <= 0) {
            throw new InsertRecommendLoseException("添加自荐信异常");
        }
        // 返回受影响的行数
        return row;
    }

    @Override
    public Integer changeRecommend(Recommend recommend) {
        // 判断recommend是否为null
        if (recommend == null) {
            throw new UpdateRecommendLoseException("修改自荐信异常");
        }
        // 完善recommend
        recommend.setUpdateTime(new Date());

        // 调用持久层接口添加数据
        Integer row = recommendMapper.updateRecommend(recommend);
        // 判断执行结果
        if (row <= 0) {
            throw new UpdateRecommendLoseException("修改自荐信异常");
        }
        // 返回受影响的行数
        return row;
    }
}
